<!doctype html>
<html lang="zh_cn" itemscope itemtype="http://schema.org/Person">
<head>
            <meta charset="utf-8">
        <!-- Site Meta Data -->
        <title>Ngrok的安装和使用(阿里云ECS主机)</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="技术博客,涉及Java/PHP/Python/Javascript等,聊聊程序,聊聊生活,聊聊事实,聊聊育儿">
        <meta name="keywords" content="编码经验,技术分享,生活积累,实事评说">
        <meta name="author" content="布丁缘">

        <link rel="shortcut icon" href="">

        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700' rel='stylesheet' type='text/css'>
        <!-- Style Meta Data -->
        <link rel="stylesheet" href="https://www.ddkiss.com/theme/css/style.css" type="text/css"/>
        <link rel="stylesheet" href="https://www.ddkiss.com/theme/css/pygments.css" type="text/css"/>

        <!-- Feed Meta Data -->
            <link href="https://www.ddkiss.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate"
                  title="一个程序员的简单生活 ATOM Feed"/>


    <meta name="keywords" content="">
    <link rel="stylesheet" href="//dn-coding-net-public-file.qbox.me/Coding-Comments/v0.1.0/default.css">
</head>

<body>
<!-- Sidebar -->
<aside>
    <center><h1><a href="/" style="color:#fff"><img id="avatar" src="/images/avatar.jpg"></a></h1></center>
        <p>一个程序员的简单生活</p>
    <br>
    <nav class="nav">
        <ul class="list-bare">
                <li><a class="nav__link" href="https://www.ddkiss.com/category/chang-yong-ji-qiao.html">常用技巧</a></li>
                <li><a class="nav__link" href="https://www.ddkiss.com/category/kai-fa-huan-jing.html">开发环境</a></li>
                <li><a class="nav__link" href="https://www.ddkiss.com/category/sheng-huo-dian-di.html">生活点滴</a></li>

                <li><a class="nav__link" href="/pages/books.html">书单</a></li>
                <li><a class="nav__link" href="/pages/movies.html">影单</a></li>
                <li><a class="nav__link" href="/pages/downloads.html">下载</a></li>
                <li><a class="nav__link" href="/pages/about.html">关于我</a></li>


        </ul>
    </nav>


    <form>
      <input type="text"  id="bdcsMain"/>
    </form>

</aside>

<!-- Content -->
<article>
  <main>
    <nav>
      <a href="/">首页</a>
      <a href="/archives.html">归档</a>
      <a href="/categories.html">分类</a>
      <a href="/tags.html">标签</a>
      <a href="/pages/about.html">关于我</a>
    </nav>
  </main>
    <section id="content">
        <article>
            <h2 class="post_title post_detail"><a href="https://www.ddkiss.com/archives/4.html" rel="bookmark"
                                                  title="Permalink to Ngrok的安装和使用(阿里云ECS主机)">Ngrok的安装和使用(阿里云ECS主机)</a></h2>

            <div class="post_list">
                <span>作者：</span><a href="https://www.ddkiss.com/author/bu-ding-yuan.html">布丁缘</a>
                <span class="post_category">分类：<a href="https://www.ddkiss.com/category/kai-fa-huan-jing.html" rel="bookmark"
                                               title="Permalink to 开发环境">开发环境</a></span>
                <span class="post_date">  时间：2017-09-24 15:52:00</span>

            </div>
            <div class="entry-content blog-post">
                <h1>前言</h1>
<p>今天想在服务器上搭建一个技术博客。但是线上服务器内存太小了，已经被塞满了。突然想到可以放在内网，搭建一个穿透服务，这样就可以部署很多Java的服务了。Java实在是对内存要求太多了。</p>
<h1>安装过程</h1>
<p>主要包括 域名解析、go安装以及ngork的安装配置</p>
<h2>配置域名</h2>
<p>ngrok只支持二级域名！一级域名测试的时候死活不行！ 另外，如果内部不用nginx转发的化，需要配置泛域名解析。</p>
<h3>安装go环境</h3>
<p>步骤如下：</p>
<p>1) 下载安装包，地址为<a href="http://www.golangtc.com/download">这里</a>。我安装的是go1.9.linux-amd64.tar.gz。然后直接将go安装包解压到服务器 /usr/local/go下</p>
<p>2) 建立软链接。　cd /usr/bin;  ln -s /usr/local/go/bin/* ./</p>
<h3>安装git</h3>
<h3>安装ngrok</h3>
<p>1) 下载源码</p>
<div class="highlight"><pre><span></span>cd /usr/local/src/
git clone https://github.com/inconshreveable/ngrok.git
export GOPATH=/usr/local/src/ngrok/
export NGROK_DOMAIN=&quot;test.ddkiss.com&quot;
cd ngrok
</pre></div>


<p>注意：这里的二级域名填你自己的，后面不再做说明</p>
<p>2) 生成证书</p>
<div class="highlight"><pre><span></span>openssl genrsa -out rootCA.key 2048

openssl req -x509 -new -nodes -key rootCA.key -subj &quot;/CN=test.ddkiss.com&quot; -days 5000 -out rootCA.pem

openssl genrsa -out device.key 2048

openssl req -new -key device.key -subj &quot;/CN=test.ddkiss.com&quot; -out device.csr

openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000

cp rootCA.pem assets/client/tls/ngrokroot.crt

cp device.crt assets/server/tls/snakeoil.crt

cp device.key assets/server/tls/snakeoil.key
</pre></div>


<p>3) 配置编译变量</p>
<p>在命令行下直接执行下面的代码</p>
<div class="highlight"><pre><span></span>GOOS=linux GOARCH=amd64

#如果是32位系统，这里 GOARCH=386
</pre></div>


<p>4) 开始编译</p>
<div class="highlight"><pre><span></span>make release-server
</pre></div>


<p>5) 验证</p>
<p>编译完成后，会在bin目录下生成一个ngrokd的文件，执行下面的命令：</p>
<div class="highlight"><pre><span></span>bin/ngrokd -domain=&quot;test.ddkiss.com&quot; -httpAddr=&quot;:8000&quot;
</pre></div>


<p>如果正确的话，会有如下输出：</p>
<div class="highlight"><pre><span></span>[root@myos ngrok]# bin/ngrokd -domain=&quot;test.ddkiss.com&quot; -httpAddr=&quot;:8000&quot;

[17:54:07 CST 2017/09/22] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified

[17:54:07 CST 2017/09/22] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:8000

[17:54:07 CST 2017/09/22] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:443

[17:54:07 CST 2017/09/22] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:4443

[17:54:07 CST 2017/09/22] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds
</pre></div>


<p>这个命令在服务器启用了一个监听端口8000，对符合　*.test.ddkiss.com　的请求进行处理</p>
<p>6) 生成客户端，默认生成的是linux下可用的</p>
<div class="highlight"><pre><span></span>make release-client
</pre></div>


<p>在本地新建一个配置文件，名字叫 ngrok.cfg，内容如下</p>
<div class="highlight"><pre><span></span>server_addr: &quot;test.ddkiss.com:4443&quot;

trust_host_root_certs: false
</pre></div>


<p>保存后执行如下命令</p>
<div class="highlight"><pre><span></span> ./ngrok -config=ngrok.cfg -subdomain test 192.168.1.101 8080
</pre></div>


<p>在浏览器中输入 test.test.ddkiss.com:8000 可以看到效果。默认会取回本地8080提供的服务。</p>
<h3>nginx配置</h3>
<p>如果我想用 blog.ddkiss.com 来访问咋办呢？　需要用nginx转发一次，配置如下：</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="n">server</span> <span class="p">{</span>

    <span class="n">listen</span>       <span class="mi">80</span><span class="p">;</span>

    <span class="n">server_name</span> <span class="n">blog</span><span class="p">.</span><span class="n">ddkiss</span><span class="p">.</span><span class="n">com</span><span class="p">;</span>

    <span class="n">location</span> <span class="o">/</span> <span class="p">{</span>

             <span class="n">proxy_pass</span> <span class="nl">http</span><span class="p">:</span><span class="c1">//test.test.ddkiss.com:8000/;</span>

             <span class="n">proxy_redirect</span> <span class="n">off</span><span class="p">;</span>

             <span class="n">client_max_body_size</span> <span class="mi">10</span><span class="n">m</span><span class="p">;</span>

             <span class="n">client_body_buffer_size</span> <span class="mi">128</span><span class="n">k</span><span class="p">;</span>

             <span class="n">proxy_connect_timeout</span> <span class="mi">90</span><span class="p">;</span>

             <span class="n">proxy_read_timeout</span> <span class="mi">90</span><span class="p">;</span>

             <span class="n">proxy_buffer_size</span> <span class="mi">4</span><span class="n">k</span><span class="p">;</span>  

             <span class="n">proxy_buffers</span> <span class="mi">6</span> <span class="mi">128</span><span class="n">k</span><span class="p">;</span>

             <span class="n">proxy_busy_buffers_size</span> <span class="mi">256</span><span class="n">k</span><span class="p">;</span>

             <span class="n">proxy_temp_file_write_size</span> <span class="mi">256</span><span class="n">k</span><span class="p">;</span>

        <span class="p">}</span>

<span class="p">}</span>
</pre></div>
</td></tr></table>

<p>注意：阿里云主机默认没有启用域名解析。必须自己去/etc/hosts 中加入本地解析。比如</p>
<div class="highlight"><pre><span></span>127.0.0.1  test.test.ddkiss.com
</pre></div>


<p>然后，就可以　blog.ddkiss.com 愉快地玩耍了！！！</p>
<h1>后记</h1>
<p>测试了下，发现自己买的1M网络带宽终于变动了。因为部署了ngrok，相当于从局域网本地获取的资源要经过两次网络。带宽的使用率确有上升。访问量小的时候倒是可以试试。</p>
<h1>参考</h1>
<ol>
<li>http://www.jianshu.com/p/ef04c7e968d7</li>
</ol>
            </div>
            <div class="post_list">
              <div><span>Tags : </span>
              </div>
            </div>
        </article>
        <div id="container"></div>
        <script type="text/javascript" src="//dn-coding-net-public-file.qbox.me/Coding-Comments/v0.1.0/gitment.min.js"></script>
        <script>
            var gitment = new Gitment({
              owner: 'whusl',
              repo: 'BlogComments',
              oauth: {
                client_id: '621866266817529fba46681653017809',
                client_secret: '14188411740b12ae52159cee9b586bf85cd54125',
              },
            })
            document.getElementById('container').appendChild(gitment.render())
          </script>
    </section>
</article>

<!-- Footer -->
    <footer>
        <p> &copy;2017-2020&nbsp;<a href="http://www.miitbeian.gov.cn/" target="_blank">鄂ICP备17020200号</a>
          Blog powered by <a href="http://getpelican.com/">Pelican</a>
        </p>
    </footer>

    <!-- Analytics -->
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?88c55edaf311dbacac56a16316b04c8b";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();
    </script>

<script type="text/javascript">(function(){document.write(unescape('%3Cdiv id="bdcs"%3E%3C/div%3E'));var bdcs = document.createElement('script');bdcs.type = 'text/javascript';bdcs.async = true;bdcs.src = 'http://znsv.baidu.com/customer_search/api/js?sid=14490611060029767912' + '&plate_url=' + encodeURIComponent(window.location.href) + '&t=' + Math.ceil(new Date()/3600000);var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(bdcs, s);})();</script>

</body>
</html>